package com.fckbug.action.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RedirectingActionForward;

import com.fckbug.service.QAService;
import com.fckbug.util.SpringContextHolder;
import com.fckbug.util.WebappUtil;
import com.fckbug.vos.Acc;


public class SaveQAction extends Action{
	QAService qaService = (QAService)SpringContextHolder.getBean("qaService");
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	            HttpServletRequest request, HttpServletResponse response) throws Exception {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		Acc acc = WebappUtil.getLoginUser(request);
		if(acc == null) {
			return new RedirectingActionForward("/user/login.html");
		}
		Integer result = qaService.saveQuestion(title, content, acc.getId());
		if(result == null) {
			return mapping.findForward("savefailed");
		}
		return new RedirectingActionForward("/q/" + result);
	}
}

